//
//  OAuthAPI.m
//  youyue
//
//  Created by oceans on 2018/8/14.
//

#import "OAuthAPI.h"
#import "HttpHeaders.h"
#import "HttpRequest.h"
#import "AESCryptObject.h"
#import <AFNetworking/AFNetworkReachabilityManager.h>

@implementation OAuthAPI

+ (void)requestAccessToken:(void (^)(NSString *))completionHandler {
    
    NSDictionary *finalPara = [HttpHeaders getTokenHttpHeaders];
    
    BOOL isloading = [[NSUserDefaults standardUserDefaults] boolForKey:@"is_token_loading"];
    if (isloading) {
        return;
    }
    
    if ([[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus] == AFNetworkReachabilityStatusReachableViaWiFi || [[AFNetworkReachabilityManager sharedManager] networkReachabilityStatus] ==  AFNetworkReachabilityStatusReachableViaWWAN) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"is_token_loading"];
    }else {
        return;
    }
    [HttpRequest postURL:kget_access_token paras:finalPara showProgress:NO successful:^(id  _Nullable result) {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"is_token_loading"];
        NSString *token = [[result objectForKey:@"data"] objectForKey:@"access_token"];
        NSString *expiretime = [[result objectForKey:@"data"] objectForKey:@"expired_at"];
        if (token && expiretime) {
            NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
            
            NSString *oldtoken = [userDef stringForKey:accessToken_userDefaults_key];
            NSString *oldexpiretime = [userDef stringForKey:accessToken_expiretime_key];
            
            NSLog(@"accessToken = %@", token);
            
            [userDef setObject:token forKey:accessToken_userDefaults_key];
            [userDef setObject:expiretime forKey:accessToken_expiretime_key];
            [userDef synchronize];
            if (oldtoken == nil || oldexpiretime == nil) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"access_token_refresh" object:nil];
            }
            if (completionHandler) {
                completionHandler(token);
            }
        }else {
            if (completionHandler) {
                completionHandler(nil);
            }
        }
    } failed:^(int code, NSString *errMsg) {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"is_token_loading"];
        if (completionHandler) {
            completionHandler(nil);
        }
    }];
}

+ (NSString*)getAccessToken {
    
    NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
    NSString *token = [userDef stringForKey:accessToken_userDefaults_key];
    NSString *expiretime = [userDef stringForKey:accessToken_expiretime_key];
    NSLog(@"accessToken = %@", token);
    if (token == nil || expiretime == nil) {
        [self requestAccessToken:nil];
        return @"";
    }else {
        NSTimeInterval now = [[NSDate date] timeIntervalSince1970];
        if ([expiretime doubleValue] <= now) {
            [self requestAccessToken:nil];
            return @"";
        }else {
            token = [AESCryptObject aes_crypt:token];
            return token;
        }
    }
}
@end
